var driver_alarm_map = function () {
   
    for(var i in this.alarms){
		var key = 
		{
			vehicle_no: this.vehicle_no,
			vehicle_color: this.vehicle_color,
			licence : this.licence,
			driverName : this.driverName,
		};
        var values = {
			platform_id : this.platform_id,
			gnsscenter_id : this.gnsscenter_id,
			alarm_time:this.alarms[i].alarm_type==2?this.alarm_time:0,
            overspeed_count:this.alarms[i].alarm_type==1?1:0,
            tired_count:this.alarms[i].alarm_type==2?1:0,
            roadoffset_count:this.alarms[i].alarm_type==11?1:0,
            over_load_count:this.alarms[i].alarm_type==10?1:0,
            prohibit_driving_count:this.alarms[i].alarm_type==14?1:0,
            nightly_3rd_count:this.alarms[i].alarm_type==15?1:0,
            alarm_count:1,
            id: this._id
        };
        emit(key, values);
    }
    
};
var driver_alarm_reduce = function (key, values) {
    const reduce_result = {
        alarm_time: [],
        overspeed_count: 0,
        roadoffset_count: 0,
        tired_count: 0,
        over_load_count: 0,
        prohibit_driving_count: 0,
        nightly_3rd_count: 0,
        alarm_count: 0,
		platform_id : 0,
		gnsscenter_id : 0
    };
    values.forEach(
        function (val) {
        reduce_result.alarm_time = reduce_result.alarm_time.concat(val.alarm_time);
        reduce_result.overspeed_count+=val.overspeed_count;
        reduce_result.roadoffset_count+=val.roadoffset_count;
        reduce_result.over_load_count+=val.over_load_count;
        reduce_result.tired_count+=val.tired_count;
        reduce_result.prohibit_driving_count+=val.prohibit_driving_count;
        reduce_result.nightly_3rd_count+=val.nightly_3rd_count;
        reduce_result.alarm_count+=val.alarm_count;
		reduce_result.platform_id=val.platform_id;
        reduce_result.gnsscenter_id=val.gnsscenter_id;
    })
    return reduce_result;
};
var driver_alarm_finalize = function (key, values) {
    var _tired_duration = 0;
    var _temp_time=0;
    var _dif_time=0;
    var _flag=0;
    for (var i in values.alarm_time) {
        if(values.alarm_time[i]!=0){
            if(_flag==0){
                _temp_time=values.alarm_time[i];
				_tired_duration = 10
                _flag=1;
            }
            _dif_time=(values.alarm_time[i]-_temp_time)/1000;
            if (_dif_time < 14400) {
                _tired_duration += _dif_time;
            }else{
				_tired_duration += 10;
			}
            _temp_time=values.alarm_time[i];
        }
        
    } 
    values.tired_duration=_tired_duration;
	values.alarm_time=[];
    return values;
};


db.runCommand({
    mapReduce: 'alarm_info_170922',
    map: driver_alarm_map,
    reduce: driver_alarm_reduce,
    finalize: driver_alarm_finalize,
    out: 'vehicle_driver_swap_alarm',
    sort:{_id:1},
	query: {
		licence:{$exists:true},
		driverName:{$exists:true}
    }
});


var ent= db.vehicle_driver_swap_alarm.find();
while(ent.hasNext()) {
     var e = ent.next();
     var _id = "170922:"+e._id.licence+":"+e._id.vehicle_no+":"+e._id.vehicle_color;
     var obj = e.value;
     var iobj = {_id:_id,vehicle_no:e._id.vehicle_no,vehicle_color:NumberInt(e._id.vehicle_color),driverName:e._id.driverName,
     alarm_count:NumberInt(obj.alarm_count),
     overspeed_count:NumberInt(obj.overspeed_count),
     roadoffset_count:NumberInt(obj.roadoffset_count),
     tired_count:NumberInt(obj.tired_count),tired_duration:NumberInt(obj.tired_duration),
     prohibit_driving_count:NumberInt(obj.prohibit_driving_count),
     nightly_3rd_count:NumberInt(obj.nightly_3rd_count),
	 platform_id:NumberInt(obj.platform_id),
	 gnsscenter_id:NumberInt(obj.gnsscenter_id),
     over_load_count:NumberInt(obj.over_load_count)
	 };  
     db.driver_alarm_swap.insert(iobj);
 }
